5.1 任务描述

前置任务:C4-004

业务模块:数据库

创建合约类

任务类型:按步骤操作

5.2 详细实现步骤

5.2.1 支出类型表

为支出类型表创建合约类,类名为ExpenditureType,如下所示:

图16 生成ExpenditureType类

Android Studio为我们生成了ExpenditureType类。在生成的类中,我们添加相应字段,并添加1个内部TableAttr类,该类定义了数据库表名、字段名和常用操作SQL语句,该类实现了BaseColumns接口。

修改后的代码如下:

仿照上例,依次为其它三个表添加代码。

package cn.edu.bistu.cs.se.accountbook;


import android.provider.BaseColumns;


/**
 * Created by hbs on 2017/11/29.
 */


public class ExpenditureType {


        private int id;
        private String typeName;
        private String note;


        public ExpenditureType(int id, String typeName, String note) {
            this.id = id;
            this.typeName = typeName;
            this.note = note;
        }


        public int getId() {
            return id;
        }


        public void setId(int id) {
            this.id = id;
        }


        public String getTypeName() {
            return typeName;
        }


        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }


        public String getNote() {
            return note;
        }


        public void setNote(String note) {
            this.note = note;
        }




    public static abstract class TableAttr implements BaseColumns {
        public static final String TABLE_NAME="expenditure_type";
        public static final String COLUMN_TYPE_NAME="type_name";
        public static final String COLUMN_NOTE="note";




        //增加
        public static final String  SQL_Insert="insert into  expenditure_type(type_name,note) values(?,?)";
        public static final String  SQL_Insert_Type_name="insert into  expenditure_type(type_name) values(?)";


        public static final String  SQL_Init="insert into  expenditure_type(type_name,note) values('餐饮',''),('购物',''),('住房',''),('交通',''),('娱乐',''),('汽车',''),('通讯',''),('零食','');";


        //更新
        public static final String  SQL_Update="update expenditure_type set type_name=?,note=? where _id=?";
        public static final String  SQL_Update_Type_name="update expenditure_type set type_name=? where _id=?";


        //删除
        public static final String  SQL_Delete_all="delete  from expenditure_type";
        public static final String  SQL_Delete_by_Id="delete from   expenditure_type where _id=?";
        public static final String  SQL_Delete_by_Type_name="delete from expenditure_type where type_name=?";


        //查询
        public static final String  SQL_Select_all="select *  from expenditure_type";
        public static final String  SQL_Select_by_Id="select *  from expenditure_type where _id=?";
        public static final String  SQL_Select_by_Type_name="select *  from expenditure_type where type_name=?";
    }
}

5.2.2 支出表

类名为Expenditures,代码如下:

package cn.edu.bistu.cs.se.accountbook;


import android.provider.BaseColumns;


import java.util.Date;


/**
 * Created by hbs on 2017-11-27.
 */


public class Expenditure {
    public Expenditure() {
    }




    private int id;
    private Date accountDate;
    private ExpenditureType type;
    private int amount;


    public Expenditure(int id, Date accountDate, ExpenditureType type, int amount) {
        this.id = id;
        this.accountDate = accountDate;
        this.type= type;
        this.amount = amount;
    }




    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public Date getAccountDate() {
        return accountDate;
    }


    public void setAccountDate(Date accountDate) {
        this.accountDate = accountDate;
    }


    public ExpenditureType getType() {
        return type;
    }


    public void setType(ExpenditureType type) {
        this.type = type;
    }


    public int getAmount() {
        return amount;
    }


    public void setAmount(int amount) {
        this.amount = amount;
    }




    public static abstract class TableAttr implements BaseColumns {
        public static final String TABLE_NAME = "expenditure";
        public static final String COLUMN_ACCOUNT_DATE = "account_date";
        public static final String COLUMN_FK_TYPE = "expenditure_type_id";
        public static final String COLUMN_AMOUNT = "amount";




        //增加
        public static final String SQL_Insert = "insert into  expenditure(account_date,expenditure_type_id,amount) values(?,?,?)";


        //更新
        public static final String SQL_Update = "update expenditure set account_date=?,expenditure_type_id=? ,amount=? where _id=?";




        //删除
        public static final String SQL_Delete_all = "delete  from expenditure";
        public static final String SQL_Delete_by_Id = "delete from   expenditure where _id=?";
        public static final String SQL_Delete_by_Type_id = "delete from expenditure where earning_type_id=?";
        public static final String SQL_Delete_by_Type_name = "delete from expenditure where earning_type_id in (select _id from expenditure_type where type_name=?)";


        //查询
        public static final String SQL_Select_all = "select *  from expenditure";
        public static final String SQL_Select_by_Id = "select *  from expenditure where _id=?";
        public static final String SQL_Select_by_Type_id = "select *  from expenditure where expenditure_type_id=?";
        public static final String SQL_Select_by_Type_name = "select * from expenditure where expenditure_type_id in (select _id from expenditure_type where type_name=?)";
        public static final String SQL_Select_Sum_By_Date="select sum(amount) from expenditure where account_date=?";
        public static final String SQL_Select_Sum="select sum(amount) from expenditure";
    }
}

5.2.3 收入类型表

类名为EarningTypes,代码如下:

package cn.edu.bistu.cs.se.accountbook;


import android.provider.BaseColumns;


/**
 * Created by hbs on 2017/11/29.
 */


public class EarningType {
    private int id;
    private String typeName;
    private String note;


    public EarningType(int id, String typeName, String note) {
        this.id = id;
        this.typeName = typeName;
        this.note = note;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getTypeName() {
        return typeName;
    }


    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }


    public String getNote() {
        return note;
    }


    public void setNote(String note) {
        this.note = note;
    }




    public static abstract class TableAttr implements BaseColumns {
        public static final String TABLE_NAME="earning_type";
        public static final String COLUMN_TYPE_NAME="type_name";
        public static final String COLUMN_NOTE="note";


        //增加
        public static final String  SQL_Insert="insert into  earning_type(type_name,note) values(?,?)";
        public static final String  SQL_Insert_Type_name="insert into  earning_type(type_name) values(?)";


        public static final String  SQL_Init="insert into  earning_type(type_name,note) values('薪资',''),('奖金','');";


        //更新
        public static final String  SQL_Update="update earning_type set type_name=?,note=? where _id=?";
        public static final String  SQL_Update_Type_name="update earning_type set type_name=? where _id=?";


        //删除
        public static final String  SQL_Delete_all="delete  from earning_type";
        public static final String  SQL_Delete_by_Id="delete from   earning_type where _id=?";
        public static final String  SQL_Delete_by_Type_name="delete from earning_type where type_name=?";


        //查询
        public static final String  SQL_Select_all="select *  from earning_type";
        public static final String  SQL_Select_by_Id="select *  from earning_type where _id=?";
        public static final String  SQL_Select_by_Type_name="select *  from earning_type where type_name=?";




    }
}

5.2.4 收入表

类名为Earnings,代码如下:

package cn.edu.bistu.cs.se.accountbook;


import android.provider.BaseColumns;


import java.util.Date;


/**
 * Created by hbs on 2017-11-27.
 */


public class Earning {




    private int id;
    private Date accountDate;
    private EarningType type;
    private int amount;


    public Earning(int id, Date accountDate, EarningType type, int amount) {
        this.id = id;
        this.accountDate = accountDate;
        this.type = type;
        this.amount = amount;
    }




    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public Date getAccountDate() {
        return accountDate;
    }


    public void setAccountDate(Date accountDate) {
        this.accountDate = accountDate;
    }


    public EarningType getType() {
        return type;
    }


    public void setType(EarningType type) {
        this.type = type;
    }


    public int getAmount() {
        return amount;
    }


    public void setAmount(int amount) {
        this.amount = amount;
    }




    public static abstract class TableAttr implements BaseColumns {
        public static final String TABLE_NAME = "earning";
        public static final String COLUMN_ACCOUNT_DATE = "account_date";
        public static final String COLUMN_FK_TYPE = "earning_type_id";
        public static final String COLUMN_AMOUNT = "amount";


        //增加
        public static final String SQL_Insert = "insert into  earning(account_date,earning_type_id,amount) values(?,?,?)";


        //更新
        public static final String SQL_Update = "update earning set account_date=?,earning_type_id=? ,amount=? where _id=?";




        //删除
        public static final String SQL_Delete_all = "delete  from earning";
        public static final String SQL_Delete_by_Id = "delete from   earning where _id=?";
        public static final String SQL_Delete_by_Type_id = "delete from earning where earning_type_id=?";
        public static final String SQL_Delete_by_Type_name = "delete from earning where earning_type_id in (select _id from earning_type where type_name=?)";


        //查询
        public static final String SQL_Select_all = "select *  from earning";
        public static final String SQL_Select_by_Id = "select *  from earning where _id=?";
        public static final String SQL_Select_by_Type_id = "select *  from earning where earning_type_id=?";
        public static final String SQL_Select_by_Type_name = "select * from earning where earning_type_id in (select _id from earning_type where type_name=?)";
        public static final String SQL_Select_Sum_By_Date="select sum(amount) from earning where account_date=?";
        public static final String SQL_Select_Sum="select sum(amount) from earning";
    }
}

results matching ""

    No results matching ""